package com.tgy.programmer_code_interview_guide._02_linkedList;

/**
 * @Author: tgy
 * @Date: 1/22/21 10:08 AM
 */
public class _01_打印两个有序链表的公共部分 {

    public static void printCommon(Node first,Node second) {


        while (first != null && second != null) {

            if (first.val < second.val) {

                first = first.next;
            }else if (first.val > second.val) {

                second = second.next;
            }else {

                System.out.println(first.val);
                first = first.next;
                second = second.next;
            }

        }

    }

    public static void printCommon01(Node first,Node second) {

        Node fNode = first,sNode = second;
        while (fNode != sNode) {

            fNode = fNode == null ? second:fNode.next;
            sNode = sNode == null ? first:sNode.next;
        }

        while (fNode != null) {

            System.out.println(fNode.val);
            fNode = fNode.next;
        }
    }

  public static void main(String[] args) {
    //
      Node first = new Node(1);
      first.next = new Node(2);
      first.next.next = new Node(3);
      first.next.next.next = new Node(4);

      Node second = new Node(5);
      second.next = new Node(6);
      second.next.next = first;

      printCommon(first,second);
  }
}
